home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / jikes-1.11 / src / unzip.cpp < prev    next >
C/C++ Source or Header  |  2000-02-23  |  27KB  |  915 lines

  1. // $Id: unzip.cpp,v 1.3 1999/08/26 15:34:10 shields Exp $
  2.  
  3. //
  4. // NOTE: Jikes incorporates compression code from the Info-ZIP
  5. // group. There are no extra charges or costs due to the use of
  6. // this code, and the original compression sources are freely
  7. // available from http://www.cdrom/com/pub/infozip/ or
  8. // ftp://ftp.cdrom.com/pub/infozip/ on the Internet.
  9. // The sole use by Jikes of this compression code is contained in the
  10. // files unzip.h and unzip.cpp, which are based on Info-ZIP's inflate.c and
  11. // associated header files.
  12. //
  13.  
  14. //
  15. // You can do whatever you like with this source file, though I would
  16. // prefer that if you modify it and redistribute it that you include
  17. // comments to that effect with your name and the date.  Thank you.
  18. // The abbreviated History list below includes the work of the
  19. // following:
  20. // M. Adler, G. Roelofs, J-l. Failly, J. Bush, C. Ghisler, A. Verheijen,
  21. // P. Kienitz, C. Spieler, S. Maxwell, J. Altman
  22. // Only the first and last entries from the original inflate.c are
  23. // reproduced here.
  24. //
  25.  
  26. //
  27. // History:
  28. // vers    date          who           what
  29. // ----  ---------  --------------  ------------------------------------
  30. //  a    ~~ Feb 92  M. Adler        used full (large, one-step) lookup table
  31. //  ...
  32. //  c16  20 Apr 97  J. Altman       added memzero(v[]) in huft_build()
  33. //
  34. #include "config.h"
  35. #include "unzip.h"
  36.  
  37. unsigned long Unzip::global_bb;                         /* bit buffer */
  38. unsigned Unzip::global_bk;                    /* bits in bit buffer */
  39.  
  40. unsigned Unzip::global_wp;  /* current position in slide */
  41. unsigned Unzip::global_hufts; /* huff memory usage */
  42. unsigned char Unzip::slide_buffer[32768];
  43. struct huft *Unzip::global_fixed_tl;    /* inflate static */
  44. struct huft *Unzip::global_fixed_td;    /* inflate static */
  45. int Unzip::global_fixed_bl,
  46.     Unzip::global_fixed_bd;
  47. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  48.     FILE *Unzip::global_file; /* file pointer for zip file */
  49. #elif defined(WIN32_FILE_SYSTEM)
  50.     char *Unzip::global_file; /* file pointer for zip file */
  51. #endif
  52. char *Unzip::global_bufferp; /* current position in output buffer */
  53.  
  54. /* Tables for deflate from PKZIP's appnote.txt. */
  55. unsigned Unzip::border[] = {    /* Order of the bit length code lengths */
  56.         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  57. unsigned short Unzip::cplens[] = {         /* Copy lengths for literal codes 257..285 */
  58.         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  59.         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  60.         /* note: see note #13 above about the 258 in this list. */
  61. unsigned short Unzip::cplext[] = {         /* Extra bits for literal codes 257..285 */
  62.         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  63.         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
  64. unsigned short Unzip::cpdist[] = {         /* Copy offsets for distance codes 0..29 */
  65.         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  66.         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  67.         8193, 12289, 16385, 24577};
  68. unsigned short Unzip::cpdext[] = {         /* Extra bits for distance codes */
  69.         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  70.         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  71.         12, 12, 13, 13};
  72.  
  73.  
  74. /* moved to consts.h (included in unzip.c), resp. funzip.c */
  75. /* And'ing with mask_bits[n] masks the lower n bits */
  76. unsigned short Unzip::mask_bits[] = {
  77.     0x0000,
  78.     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  79.     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  80. };
  81.  
  82. int Unzip::lbits = 9;           /* bits in base literal/length lookup table */
  83. int Unzip::dbits = 6;           /* bits in base distance lookup table */
  84.  
  85. struct huft *fixed_tl = (struct huft *) 0;
  86.  
  87. int Unzip::huft_build(unsigned *b,unsigned n, unsigned s, unsigned short *d, unsigned short *e, struct huft **t, int *m)
  88. /*unsigned *b             code lengths in bits (all assumed <= BMAX) */
  89. /*unsigned n              number of codes (assumed <= N_MAX) */
  90. /*unsigned s              number of simple-valued codes (0..s-1) */
  91. /* unsigned short *d                  list of base values for non-simple codes */
  92. /*ush *e                  list of extra bits for non-simple codes */
  93. /*struct huft **t         result: starting table */
  94. /*int *m                  maximum lookup bits, returns actual */
  95. /* Given a list of code lengths and a maximum table size, make a set of
  96.    tables to decode that set of codes.  Return zero on success, one if
  97.    the given code set is incomplete (the tables are still built in this
  98.    case), two if the input is invalid (all zero length codes or an
  99.    oversubscribed set of lengths), and three if not enough memory.
  100.    The code with value 256 is special, and the tables are constructed
  101.    so that no bits beyond that code are fetched when that code is
  102.    decoded. */
  103. {
  104.   unsigned a;                   /* counter for codes of length k */
  105.   unsigned c[BMAX+1];           /* bit length count table */
  106.   unsigned el;                  /* length of EOB code (value 256) */
  107.   unsigned f;                   /* i repeats in table every f entries */
  108.   int g;                        /* maximum code length */
  109.   int h;                        /* table level */
  110.   register unsigned i;          /* counter, current code */
  111.   register unsigned j;          /* counter */
  112.   register int k;               /* number of bits in current code */
  113.   int lx[BMAX+1];               /* memory for l[-1..BMAX-1] */
  114.   int *l = lx+1;                /* stack of bits per table */
  115.   register unsigned *p;         /* pointer into c[], b[], or v[] */
  116.   register struct huft *q;      /* points to current table */
  117.   struct huft r;                /* table entry for structure assignment */
  118.   struct huft *u[BMAX];         /* table stack */
  119.   unsigned v[N_MAX];            /* values in order of bit length */
  120.   register int w;               /* bits before this table == (l * h) */
  121.   unsigned x[BMAX+1];           /* bit offsets, then code stack */
  122.   unsigned *xp;                 /* pointer into x */
  123.   int y;                        /* number of dummy codes added */
  124.   unsigned z;                   /* number of entries in current table */
  125.  
  126.  
  127.   /* Generate counts for each bit length */
  128.   el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */
  129.   memset((char *)c,0, sizeof(c));
  130.   p = b;  i = n;
  131.   do {
  132.     c[*p]++; p++;               /* assume all entries <= BMAX */
  133.   } while (--i);
  134.   if (c[0] == n)                /* null input--all zero length codes */
  135.   {
  136.     *t = (struct huft *)0;
  137.     *m = 0;
  138.     return 0;
  139.   }
  140.  
  141.  
  142.   /* Find minimum and maximum length, bound *m by those */
  143.   for (j = 1; j <= BMAX; j++)
  144.     if (c[j])
  145.       break;
  146.   k = j;                        /* minimum code length */
  147.   if ((unsigned)*m < j)
  148.     *m = j;
  149.   for (i = BMAX; i; i--)
  150.     if (c[i])
  151.       break;
  152.   g = i;                        /* maximum code length */
  153.   if ((unsigned)*m > i)
  154.     *m = i;
  155.  
  156.  
  157.   /* Adjust last length count to fill out codes, if needed */
  158.   for (y = 1 << j; j < i; j++, y <<= 1)
  159.     if ((y -= c[j]) < 0)
  160.       return 2;                 /* bad input: more codes than bits */
  161.   if ((y -= c[i]) < 0)
  162.     return 2;
  163.   c[i] += y;
  164.  
  165.  
  166.   /* Generate starting offsets into the value table for each length */
  167.   x[1] = j = 0;
  168.   p = c + 1;  xp = x + 2;
  169.   while (--i) {                 /* note that i == g from above */
  170.     *xp++ = (j += *p++);
  171.   }
  172.  
  173.  
  174.   /* Make a table of values in order of bit lengths */
  175.   memset((char *)v,0, sizeof(v));
  176.   p = b;  i = 0;
  177.   do {
  178.     if ((j = *p++) != 0)
  179.       v[x[j]++] = i;
  180.   } while (++i < n);
  181.   n = x[g];                     /* set n to length of v */
  182.  
  183.  
  184.   /* Generate the Huffman codes and for each, make the table entries */
  185.   x[0] = i = 0;                 /* first Huffman code is zero */
  186.   p = v;                        /* grab values in bit order */
  187.   h = -1;                       /* no tables yet--level -1 */
  188.   w = l[-1] = 0;                /* no bits decoded yet */
  189.   u[0] = (struct huft *)0;   /* just to keep compilers happy */
  190.   q = (struct huft *)0;      /* ditto */
  191.   z = 0;                        /* ditto */
  192.  
  193.   /* go through the bit lengths (k already is bits in shortest code) */
  194.   for (; k <= g; k++)
  195.   {
  196.     a = c[k];
  197.     while (a--)
  198.     {
  199.       /* here i is the Huffman code of length k bits for value *p */
  200.       /* make tables up to required level */
  201.       while (k > w + l[h])
  202.       {
  203.         w += l[h++];            /* add bits already decoded */
  204.  
  205.         /* compute minimum size table less than or equal to *m bits */
  206.         z = (z = g - w) > (unsigned)*m ? *m : z;        /* upper limit */
  207.         if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
  208.         {                       /* too few codes for k-w bit table */
  209.           f -= a + 1;           /* deduct codes from patterns left */
  210.           xp = c + k;
  211.           while (++j < z)       /* try smaller tables up to z bits */
  212.           {
  213.             if ((f <<= 1) <= *++xp)
  214.               break;            /* enough codes to use up j bits */
  215.             f -= *xp;           /* else deduct codes from patterns */
  216.           }
  217.         }
  218.         if ((unsigned)w + j > el && (unsigned)w < el)
  219.           j = el - w;           /* make EOB code end at table */
  220.         z = 1 << j;             /* table entries for j-bit table */
  221.         l[h] = j;               /* set table size in stack */
  222.  
  223.         /* allocate and link in new table */
  224.         if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == (struct huft *)0)
  225.         {
  226.           if (h)
  227.             huft_free(u[0]);
  228.           return 3;             /* not enough memory */
  229.         }
  230.         global_hufts += z + 1;         /* track memory usage */
  231.         *t = q + 1;             /* link to list for huft_free() */
  232.         *(t = &(q->v.t)) = (struct huft *)0;
  233.         u[h] = ++q;             /* table starts after link */
  234.  
  235.         /* connect to last table, if there is one */
  236.         if (h)
  237.         {
  238.           x[h] = i;             /* save pattern for backing up */
  239.           r.b = (unsigned char)l[h-1];    /* bits to dump before this table */
  240.           r.e = (unsigned char)(16 + j);  /* bits in this table */
  241.           r.v.t = q;            /* pointer to this table */
  242.           j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
  243.           u[h-1][j] = r;        /* connect to last table */
  244.         }
  245.       }
  246.  
  247.       /* set up table entry in r */
  248.       r.b = (unsigned char)(k - w);
  249.       if (p >= v + n)
  250.         r.e = 99;               /* out of values--invalid code */
  251.       else if (*p < s)
  252.       {
  253.         r.e = (unsigned char)(*p < 256 ? 16 : 15);  /* 256 is end-of-block code */
  254.         r.v.n = (unsigned short)*p++;                /* simple code is just the value */
  255.       }
  256.       else
  257.       {
  258.         r.e = (unsigned char)e[*p - s];   /* non-simple--look up in lists */
  259.         r.v.n = d[*p++ - s];
  260.       }
  261.  
  262.       /* fill code-like entries with r */
  263.       f = 1 << (k - w);
  264.       for (j = i >> w; j < z; j += f)
  265.         q[j] = r;
  266.  
  267.       /* backwards increment the k-bit code i */
  268.       for (j = 1 << (k - 1); i & j; j >>= 1)
  269.         i ^= j;
  270.       i ^= j;
  271.  
  272.       /* backup over finished tables */
  273.       while ((i & ((1 << w) - 1)) != x[h])
  274.         w -= l[--h];            /* don't need to update q */
  275.     }
  276.   }
  277.  
  278.  
  279.   /* return actual size of base table */
  280.   *m = l[0];
  281.  
  282.  
  283.   /* Return true (1) if we were given an incomplete table */
  284.   return y != 0 && g != 1;
  285. }
  286.  
  287.  
  288. int Unzip::huft_free(struct huft *t)
  289. /*struct huft *t          table to free */
  290. /* Free the malloc'ed tables built by huft_build(), which makes a linked
  291.    list of the tables it made, with the links in a dummy first entry of
  292.    each table. */
  293. {
  294.   register struct huft *p, *q;
  295.  
  296.  
  297.   /* Go through linked list, freeing from the malloced (t[-1]) address. */
  298.   p = t;
  299.   while (p != (struct huft *)0)
  300.   {
  301.     q = (--p)->v.t;
  302.     free(p);
  303.     p = q;
  304.   }
  305.   return 0;
  306. }
  307.  
  308.  
  309. int Unzip::inflate_codes(struct huft *tl,struct huft * td, int  bl,int bd)
  310. /*struct huft *tl, *td literal/length and distance decoder tables */
  311. /* int bl, bd;              number of bits decoded by tl[] and td[] */
  312. /* inflate (decompress) the codes in a deflated (compressed) block.
  313.    Return an error code or zero if it all goes ok. */
  314. {
  315.   register unsigned e;  /* table entry flag/number of extra bits */
  316.   unsigned n, d;        /* length and index for copy */
  317.   unsigned w;           /* current window position */
  318.   struct huft *t;       /* pointer to table entry */
  319.   unsigned ml, md;      /* masks for bl and bd bits */
  320.   register unsigned long b;       /* bit buffer */
  321.   register unsigned k;  /* number of bits in bit buffer */
  322.  
  323.  
  324.   /* make local copies of globals */
  325.   b = global_bb;                       /* initialize bit buffer */
  326.   k = global_bk;
  327.   w = global_wp;                       /* initialize window position */
  328.  
  329.  
  330.   /* inflate the coded data */
  331.   ml = mask_bits[bl];           /* precompute masks for speed */
  332.   md = mask_bits[bd];
  333.   while (1)                     /* do until end of block */
  334.   {
  335.     NEEDBITS((unsigned)bl)
  336.     if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
  337.       do {
  338.         if (e == 99)
  339.           return 1;
  340.         DUMPBITS(t->b)
  341.         e -= 16;
  342.         NEEDBITS(e)
  343.       } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
  344.     DUMPBITS(t->b)
  345.     if (e == 16)                /* then it's a literal */
  346.     {
  347.       slide_buffer[w++] = (unsigned char)t->v.n;
  348.       if (w == wsize)
  349.       {
  350.         FLUSH(w);
  351.         w = 0;
  352.       }
  353.     }
  354.     else                        /* it's an EOB or a length */
  355.     {
  356.       /* exit if end of block */
  357.       if (e == 15)
  358.         break;
  359.  
  360.       /* get length of block to copy */
  361.       NEEDBITS(e)
  362.       n = t->v.n + ((unsigned)b & mask_bits[e]);
  363.       DUMPBITS(e);
  364.  
  365.       /* decode distance of block to copy */
  366.       NEEDBITS((unsigned)bd)
  367.       if ((e = (t = td + ((unsigned)b & md))->e) > 16)
  368.         do {
  369.           if (e == 99)
  370.             return 1;
  371.           DUMPBITS(t->b)
  372.           e -= 16;
  373.           NEEDBITS(e)
  374.         } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
  375.       DUMPBITS(t->b)
  376.       NEEDBITS(e)
  377.       d = w - t->v.n - ((unsigned)b & mask_bits[e]);
  378.       DUMPBITS(e)
  379.  
  380.       /* do the copy */
  381.       do {
  382.           n -= (e = (e = wsize - ((d &= wsize-1) > w ? d : w)) > n ? n : e);
  383. #ifndef NOMEMCPY
  384.         if (w - d >= e)         /* (this test assumes unsigned comparison) */
  385.         {
  386.           memmove(slide_buffer + w, slide_buffer + d, e);
  387.           w += e;
  388.           d += e;
  389.         }
  390.         else                    /* do it slowly to avoid memcpy() overlap */
  391. #endif /* !NOMEMCPY */
  392.           do {
  393.             slide_buffer[w++] = slide_buffer[d++];
  394.           } while (--e);
  395.         if (w == wsize)
  396.         {
  397.           FLUSH(w);
  398.           w = 0;
  399.         }
  400.       } while (n);
  401.     }
  402.   }
  403.  
  404.  
  405.   /* restore the globals from the locals */
  406.   global_wp = w;                       /* restore global window pointer */
  407.   global_bb = b;                       /* restore global bit buffer */
  408.   global_bk = k;
  409.  
  410.  
  411.   /* done */
  412.   return 0;
  413. }
  414.  
  415.  
  416. int Unzip::inflate_stored()
  417. /* "decompress" an inflated type 0 (stored) block. */
  418. {
  419.   unsigned n;           /* number of bytes in block */
  420.   unsigned w;           /* current window position */
  421.   register unsigned long b;       /* bit buffer */
  422.   register unsigned k;  /* number of bits in bit buffer */
  423.  
  424.  
  425.   /* make local copies of globals */
  426.   Trace((stderr, "\nstored block"));
  427.   b = global_bb;                       /* initialize bit buffer */
  428.   k = global_bk;
  429.   w = global_wp;                       /* initialize window position */
  430.  
  431.  
  432.   /* go to byte boundary */
  433.   n = k & 7;
  434.   DUMPBITS(n);
  435.  
  436.  
  437.   /* get the length and its complement */
  438.   NEEDBITS(16)
  439.   n = ((unsigned)b & 0xffff);
  440.   DUMPBITS(16)
  441.   NEEDBITS(16)
  442.   if (n != (unsigned)((~b) & 0xffff))
  443.     return 1;                   /* error in compressed data */
  444.   DUMPBITS(16)
  445.  
  446.  
  447.   /* read and output the compressed data */
  448.   while (n--)
  449.   {
  450.     NEEDBITS(8)
  451.     slide_buffer[w++] = (unsigned char)b;
  452.     if (w == wsize)
  453.     {
  454.       FLUSH(w);
  455.       w = 0;
  456.     }
  457.     DUMPBITS(8)
  458.   }
  459.  
  460.  
  461.   /* restore the globals from the locals */
  462.   global_wp = w;                       /* restore global window pointer */
  463.   global_bb = b;                       /* restore global bit buffer */
  464.   global_bk = k;
  465.   return 0;
  466. }
  467.  
  468.  
  469. int Unzip::inflate_fixed()
  470. /* decompress an inflated type 1 (fixed Huffman codes) block.  We should
  471.    either replace this with a custom decoder, or at least precompute the
  472.    Huffman tables. */
  473. {
  474.   /* if first time, set up tables for fixed blocks */
  475.   Trace((stderr, "\nliteral block"));
  476.   if (global_fixed_tl == (struct huft *)0)
  477.   {
  478.     int i;                /* temporary variable */
  479.     unsigned l[288];      /* length list for huft_build */
  480.  
  481.     /* literal table */
  482.     for (i = 0; i < 144; i++)
  483.       l[i] = 8;
  484.     for (; i < 256; i++)
  485.       l[i] = 9;
  486.     for (; i < 280; i++)
  487.       l[i] = 7;
  488.     for (; i < 288; i++)          /* make a complete, but wrong code set */
  489.       l[i] = 8;
  490.     global_fixed_bl = 7;
  491.     if ((i = huft_build(l, 288, 257, cplens, cplext,
  492.                         &global_fixed_tl, &global_fixed_bl)) != 0)
  493.     {
  494.       global_fixed_tl = (struct huft *)0;
  495.       return i;
  496.     }
  497.  
  498.     /* distance table */
  499.     for (i = 0; i < 30; i++)      /* make an incomplete code set */
  500.       l[i] = 5;
  501.     global_fixed_bd = 5;
  502.     if ((i = huft_build(l, 30, 0, cpdist, cpdext,
  503.                         &global_fixed_td, &global_fixed_bd)) > 1)
  504.     {
  505.       huft_free(global_fixed_tl);
  506.       global_fixed_tl = (struct huft *)0;
  507.       return i;
  508.     }
  509.   }
  510.  
  511.   /* decompress until an end-of-block code */
  512.   return inflate_codes(global_fixed_tl, global_fixed_td,
  513.                              global_fixed_bl, global_fixed_bd) != 0;
  514. }
  515.  
  516.  
  517.  
  518. int Unzip::inflate_dynamic()
  519. /* decompress an inflated type 2 (dynamic Huffman codes) block. */
  520. {
  521.   int i;                /* temporary variables */
  522.   unsigned j;
  523.   unsigned l;           /* last length */
  524.   unsigned m;           /* mask for bit lengths table */
  525.   unsigned n;           /* number of lengths to get */
  526.   struct huft *tl;      /* literal/length code table */
  527.   struct huft *td;      /* distance code table */
  528.   int bl;               /* lookup bits for tl */
  529.   int bd;               /* lookup bits for td */
  530.   unsigned nb;          /* number of bit length codes */
  531.   unsigned nl;          /* number of literal/length codes */
  532.   unsigned nd;          /* number of distance codes */
  533. #ifdef PKZIP_BUG_WORKAROUND
  534.   unsigned ll[288+32]; /* literal/length and distance code lengths */
  535. #else
  536.   unsigned ll[286+30]; /* literal/length and distance code lengths */
  537. #endif
  538.   register unsigned long b;       /* bit buffer */
  539.   register unsigned k;  /* number of bits in bit buffer */
  540.  
  541.  
  542.   /* make local bit buffer */
  543.   Trace((stderr, "\ndynamic block"));
  544.   b = global_bb;
  545.   k = global_bk;
  546.  
  547.  
  548.   /* read in table lengths */
  549.   NEEDBITS(5)
  550.   nl = 257 + ((unsigned)b & 0x1f);      /* number of literal/length codes */
  551.   DUMPBITS(5)
  552.   NEEDBITS(5)
  553.   nd = 1 + ((unsigned)b & 0x1f);        /* number of distance codes */
  554.   DUMPBITS(5)
  555.   NEEDBITS(4)
  556.   nb = 4 + ((unsigned)b & 0xf);         /* number of bit length codes */
  557.   DUMPBITS(4)
  558. #ifdef PKZIP_BUG_WORKAROUND
  559.   if (nl > 288 || nd > 32)
  560. #else
  561.   if (nl > 286 || nd > 30)
  562. #endif
  563.     return 1;                   /* bad lengths */
  564.  
  565.  
  566.   /* read in bit-length-code lengths */
  567.   for (j = 0; j < nb; j++)
  568.   {
  569.     NEEDBITS(3)
  570.     ll[border[j]] = (unsigned)b & 7;
  571.     DUMPBITS(3)
  572.   }
  573.   for (; j < 19; j++)
  574.     ll[border[j]] = 0;
  575.  
  576.  
  577.   /* build decoding table for trees--single level, 7 bit lookup */
  578.   bl = 7;
  579.   i = huft_build(ll, 19, 19, 0, 0, &tl, &bl);
  580.   if (bl == 0)                        /* no bit lengths */
  581.     i = 1;
  582.   if (i)
  583.   {
  584.     if (i == 1)
  585.       huft_free(tl);
  586.     return i;                   /* incomplete code set */
  587.   }
  588.  
  589.  
  590.   /* read in literal and distance code lengths */
  591.   n = nl + nd;
  592.   m = mask_bits[bl];
  593.   i = l = 0;
  594.   while ((unsigned)i < n)
  595.   {
  596.     NEEDBITS((unsigned)bl)
  597.     j = (td = tl + ((unsigned)b & m))->b;
  598.     DUMPBITS(j)
  599.     j = td->v.n;
  600.     if (j < 16)                 /* length of code in bits (0..15) */
  601.       ll[i++] = l = j;          /* save last length in l */
  602.     else if (j == 16)           /* repeat last length 3 to 6 times */
  603.     {
  604.       NEEDBITS(2)
  605.       j = 3 + ((unsigned)b & 3);
  606.       DUMPBITS(2)
  607.       if ((unsigned)i + j > n)
  608.         return 1;
  609.       while (j--)
  610.         ll[i++] = l;
  611.     }
  612.     else if (j == 17)           /* 3 to 10 zero length codes */
  613.     {
  614.       NEEDBITS(3)
  615.       j = 3 + ((unsigned)b & 7);
  616.       DUMPBITS(3)
  617.       if ((unsigned)i + j > n)
  618.         return 1;
  619.       while (j--)
  620.         ll[i++] = 0;
  621.       l = 0;
  622.     }
  623.     else                        /* j == 18: 11 to 138 zero length codes */
  624.     {
  625.       NEEDBITS(7)
  626.       j = 11 + ((unsigned)b & 0x7f);
  627.       DUMPBITS(7)
  628.       if ((unsigned)i + j > n)
  629.         return 1;
  630.       while (j--)
  631.         ll[i++] = 0;
  632.       l = 0;
  633.     }
  634.   }
  635.  
  636.  
  637.   /* free decoding table for trees */
  638.   huft_free(tl);
  639.  
  640.  
  641.   /* restore the global bit buffer */
  642.   global_bb = b;
  643.   global_bk = k;
  644.  
  645.  
  646.   /* build the decoding tables for literal/length and distance codes */
  647.   bl = lbits;
  648.   i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl);
  649.   if (bl == 0)                        /* no literals or lengths */
  650.     i = 1;
  651.   if (i)
  652.   {
  653.     if (i == 1) {
  654.       MESSAGE((unsigned char *)"(incomplete l-tree)  ", 21L, 1);
  655.       huft_free(tl);
  656.     }
  657.     return i;                   /* incomplete code set */
  658.   }
  659.   bd = dbits;
  660.   i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd);
  661.   if (bd == 0 && nl > 257)    /* lengths but no distances */
  662.   {
  663.     MESSAGE((unsigned char *)"(incomplete d-tree)  ", 21L, 1);
  664.     huft_free(tl);
  665.     return 1;
  666.   }
  667.   if (i == 1) {
  668. #ifdef PKZIP_BUG_WORKAROUND
  669.     i = 0;
  670. #else
  671.     if (!G.qflag)
  672.       MESSAGE((unsigned char *)"(incomplete d-tree)  ", 21L, 1);
  673.     huft_free(td);
  674. #endif
  675.   }
  676.   if (i)
  677.   {
  678.     huft_free(tl);
  679.     return i;
  680.   }
  681.  
  682.  
  683.   /* decompress until an end-of-block code */
  684.   if (inflate_codes(tl, td, bl, bd))
  685.     return 1;
  686.  
  687.  
  688.   /* free the decoding tables, return */
  689.   huft_free(tl);
  690.   huft_free(td);
  691.   return 0;
  692. }
  693.  
  694.  
  695.  
  696. int Unzip::inflate_block(int *e)
  697. /*  int *e                last block flag */
  698. /* decompress an inflated block */
  699. {
  700.   unsigned t;           /* block type */
  701.   register unsigned long b;       /* bit buffer */
  702.   register unsigned k;  /* number of bits in bit buffer */
  703.  
  704.  
  705.   /* make local bit buffer */
  706.   b = global_bb;
  707.   k = global_bk;
  708.  
  709.  
  710.   /* read in last block bit */
  711.   NEEDBITS(1)
  712.   *e = (int)b & 1;
  713.   DUMPBITS(1)
  714.  
  715.  
  716.   /* read in block type */
  717.   NEEDBITS(2)
  718.   t = (unsigned)b & 3;
  719.   DUMPBITS(2)
  720.  
  721.  
  722.   /* restore the global bit buffer */
  723.   global_bb = b;
  724.   global_bk = k;
  725.  
  726.  
  727.   /* inflate that block type */
  728.   if (t == 2)
  729.     return inflate_dynamic();
  730.   if (t == 0)
  731.     return inflate_stored();
  732.   if (t == 1)
  733.     return inflate_fixed();
  734.  
  735.  
  736.   /* bad block type */
  737.   return 2;
  738. }
  739.  
  740.  
  741. int Unzip::inflate_free()
  742. {
  743.   if (global_fixed_tl != (struct huft *)0)
  744.   {
  745.     huft_free(global_fixed_td);
  746.     huft_free(global_fixed_tl);
  747.     global_fixed_td = global_fixed_tl = (struct huft *)0;
  748.   }
  749.   return 0;
  750. }
  751.  
  752. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  753.     int Unzip::unzip8(FILE * zipfile, char *buffer)
  754. #elif defined(WIN32_FILE_SYSTEM)
  755.     int Unzip::unzip8(char *zipfile, char *buffer)
  756. #endif
  757. /* decompress an inflated entry */
  758. {
  759.   int e;                /* last block flag */
  760.   int r;                /* result code */
  761.   unsigned h;           /* maximum struct huft's malloc'ed */
  762.  
  763.   /* initialize window, bit buffer */
  764.   global_wp = 0;
  765.   global_bk = 0;
  766.   global_bb = 0;
  767.   global_file = zipfile;
  768.   global_bufferp = buffer;
  769.  
  770.  
  771.   /* decompress until the last block */
  772.   h = 0;
  773.   do {
  774.     global_hufts = 0;
  775.     if ((r = inflate_block(&e)) != 0)
  776.       return r;
  777.     if (global_hufts > h)
  778.       h = global_hufts;
  779.   } while (!e);
  780.  
  781.  
  782.   /* flush out slide_buffer */
  783.   FLUSH(global_wp);
  784.  
  785.  
  786.   /* return success */
  787.   Trace((stderr, "\n%u bytes in Huffman tables (%d/entry)\n",
  788.          h * sizeof(struct huft), sizeof(struct huft)));
  789.   return 0;
  790. }
  791.  
  792.  
  793. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  794.     int Unzip::UncompressFile0(FILE *zipfile, char *buffer, long buffer_length)
  795.     {
  796.         fread(buffer, sizeof(char), buffer_length, zipfile);
  797.         return 1;
  798.     }
  799.  
  800.  
  801.     int Unzip::UncompressFile1(FILE *zipfile, char *buffer, long buffer_length)
  802.     {
  803.         return 0;
  804.     }
  805.  
  806.  
  807.     int Unzip::UncompressFile2(FILE *zipfile, char *buffer, long buffer_length)
  808.     {
  809.         return 0;
  810.     }
  811.  
  812.  
  813.     int Unzip::UncompressFile3(FILE *zipfile, char *buffer, long buffer_length)
  814.     {
  815.         return 0;
  816.     }
  817.  
  818.  
  819.     int Unzip::UncompressFile4(FILE *zipfile, char *buffer, long buffer_length)
  820.     {
  821.         return 0;
  822.     }
  823.  
  824.  
  825.     int Unzip::UncompressFile5(FILE *zipfile, char *buffer, long buffer_length)
  826.     {
  827.         return 0;
  828.     }
  829.  
  830.  
  831.     int Unzip::UncompressFile6(FILE *zipfile, char *buffer, long buffer_length)
  832.     {
  833.         return 0;
  834.     }
  835.  
  836.  
  837.     int Unzip::UncompressFile7(FILE *zipfile, char *buffer, long buffer_length)
  838.     {
  839.         return 0;
  840.     }
  841.  
  842.     int Unzip::UncompressFile8(FILE *zipfile, char *buffer, long buffer_length)
  843.     {
  844.         int rc = Unzip::unzip8(zipfile, buffer); /* Use Unzip routine to unpack */
  845.         return (rc == 0);
  846.     }
  847.  
  848.  
  849.     int Unzip::UncompressFile9(FILE *zipfile, char *buffer, long buffer_length)
  850.     {
  851.         return 0;
  852.     }
  853. #elif defined(WIN32_FILE_SYSTEM)
  854.     int Unzip::UncompressFile0(char *zipfile_buffer, char *buffer, long buffer_length)
  855.     {
  856.         memmove(buffer, zipfile_buffer, buffer_length * sizeof(char));
  857.         return 1;
  858.     }
  859.  
  860.  
  861.     int Unzip::UncompressFile1(char *zipfile_buffer, char *buffer, long buffer_length)
  862.     {
  863.         return 0;
  864.     }
  865.  
  866.  
  867.     int Unzip::UncompressFile2(char *zipfile_buffer, char *buffer, long buffer_length)
  868.     {
  869.         return 0;
  870.     }
  871.  
  872.  
  873.     int Unzip::UncompressFile3(char *zipfile_buffer, char *buffer, long buffer_length)
  874.     {
  875.         return 0;
  876.     }
  877.  
  878.  
  879.     int Unzip::UncompressFile4(char *zipfile_buffer, char *buffer, long buffer_length)
  880.     {
  881.         return 0;
  882.     }
  883.  
  884.  
  885.     int Unzip::UncompressFile5(char *zipfile_buffer, char *buffer, long buffer_length)
  886.     {
  887.         return 0;
  888.     }
  889.  
  890.  
  891.     int Unzip::UncompressFile6(char *zipfile_buffer, char *buffer, long buffer_length)
  892.     {
  893.         return 0;
  894.     }
  895.  
  896.  
  897.     int Unzip::UncompressFile7(char *zipfile_buffer, char *buffer, long buffer_length)
  898.     {
  899.         return 0;
  900.     }
  901.  
  902.     int Unzip::UncompressFile8(char *zipfile_buffer, char *buffer, long buffer_length)
  903.     {
  904.         int rc = Unzip::unzip8(zipfile_buffer, buffer); /* Use Unzip routine to unpack */
  905.         return (rc == 0);
  906.     }
  907.  
  908.  
  909.     int Unzip::UncompressFile9(char *zipfile_buffer, char *buffer, long buffer_length)
  910.     {
  911.         return 0;
  912.     }
  913. #endif
  914.  
  915.